<template>
	<view class="layout">
		<view>
			<view class="header-top">

			</view>
			<view class="header-top acea-row row-between-wrapper row-middle">
				<view>
					<text class="iconfont icon-fanhui2 px-20" style="color: #fff;" @tap="returns"></text>
				</view>
				<!-- <view class="header-top-tittle">我的发布 - {{categoryList[categoryOn].name}}{{types[typeOn].name}}</view> -->
				<view class="header-top-tittle">我的发布</view>
				<view @click="toPage('/pages/releases/subletOfShop/subletOfShop')">
					<img src="../static/images/add.png" style='width: 40rpx; height: 40rpx;margin-top: 15rpx;'>
				</view>
			</view>
			<view class="acea-row row-middle row-between nav-bg">
				<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
					:scroll-left="tabLeft" show-scrollbar="true">
					<view class="item" :style='"width:"+isWidth+"px"' :data-index="index"
						:class="[index===categoryOn?'item-on':'',index>4?'item-mr-l':'']"
						v-for="(item,index) in categoryList" :key="index" :id="'id'+index"
						@click="categoryNavClick(item,index)">
						<view class="acea-row row-middle row-center img">
							<image :src="'../static/images/'+item.image" mode=""></image>
						</view>
						<view class="name">
							{{item.name}}
						</view>
					</view>
				</scroll-view>
			</view>

			<view class="type-bg acea-row row-middle row-between" style="width: 100%;">
				<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
					show-scrollbar="true" :scroll-left="tabRight">
					<view class="type" :style='"width:"+widthIng+"px"' :class="typeOn==index?'type-on':''"
						v-for="(item,index) in types" :key="index" @click="typeNavClick(item,index)">
						{{item.name}}
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="scroll">
			<scroll-view :scroll-top="scrollTopList" scroll-y="true" class="scroll-Y" @scrolltolower="lower">
				<block v-for="(item,index) in dataList" :key="index">
					<lease v-if="![3,5].includes(typeOn) && ['0','1','2'].includes(item.status)" :item="item"
						@to-topping="toTopping" @tgMeth="tgMeth" @setMeth="setMeth" @visitor="toVisitor" @updateItem="((k,v)=>$set(dataList[index],k,v))"
						@toVip="toVip" />
					<askRent v-else :item="item" @jumpPage="jumpPage(item)" @to-topping="toTopping" @tgMeth="tgMeth"  @updateItem="((k,v)=>$set(dataList[index],k,v))"
						@setMeth="setMeth" @visitor="toVisitor" @toVip="toVip" />
							
				</block>
				<view style="padding-top: 15px; text-align: center;" v-if="dataList.length<1">暂无数据</view>
			</scroll-view>
		</view>

		<!-- <view class="type-bg acea-row row-middle row-between">
			<view class="type" :class="typeOn==index?'type-on':''" v-for="(item,index) in types" :key="index" @click="typeNavClick(item,index)">
				{{item.name}}
			</view>
		</view> -->
	

		<!-- 出租 -->
		<topping :show="linkShow" :link-id="linkId" :types="linkTypes" @close="linkShow=false" @opear="toOpear" :storeCategory="infoStoreCategory" :status="infoStatus" />
		<setting ref="setting" :show="setShow" @close="setShow=false" :item="settingItem" @opear="toOpear"
			@toChange="settingchange" />
		<view class="bottom acea-row row-center" @click="onRefresh">
			一键刷新
		</view>
	</view>
</template>

<script>
	import lease from "../components/lease.vue"
	import askRent from "../components/askRent.vue"
	import topping from '../components/topping.vue'
	import setting from '../components/setting.vue'
	import {
		AllListApi,
		OnListApi,
		getOnGroupListApi,
		BuyListApi,
		onBuyListApi
	} from '@/api/subletOfShop.js'
	import {
		getCategory,
		toPage,
		showLoading
	} from '@/filters/commFilter.js'
	import {
		reardTrusteeship
	} from '@/api/api.js'

	export default {
		components: {
			lease,
			askRent,
			topping,
			setting
		},
		data() {
			return {
				tabRight: 0,
				widthIng: 60,
				isWidth: 62,
				isLeft: 0,
				linkTypes: '',
				scrollTopList: 0,
				tabLeft: 0,
				linkShow: false,
				setShow: false,
				infoStoreCategory:'',
				infoStatus:'',
				types: [{
						name: '全部',
						type: ""
					},
					{
						name: '出租',
						type: 0
					},
					{
						name: '转让',
						type: 2
					}, {
						name: '求租',
						type: ''
					}, {
						name: '出售',
						type: 1
					}, {
						name: '求购',
						type: ''
					}
				],
				typeOn: 0,
				categoryList: [{
					name: '全部',
					image: 'all.png',
					type: ''
				}, {
					name: '商铺',
					image: 'nav_01.png',
					type: 0,
				}, {
					name: '房屋',
					image: 'nav_06.png',
					type: 6
				}, {
					name: '写字楼',
					image: 'nav_02.png',
					type: 1,
				}, {
					name: '公寓',
					image: 'nav_02.png',
					type: 2,
				}, {
					name: '厂房',
					image: 'nav_03.png',
					type: 3,
				}, {
					name: '仓库',
					image: 'nav_04.png',
					type: 4
				}, {
					name: '土地',
					image: 'nav_05.png',
					type: 5,
				}],
				categoryOn: 0,
				dataList: [],
				page: 1,
				limit: 8,
				linkId: 0,
				settingItem: {},
			}
		},
		created() {
			// showLoading()
			this.initList()
		},
		// onReachBottom() {
		// 	this.page++;
		// 	this.initList()
		// },
		computed: {
			//当前的类型type
			categoryType() {
				return this.categoryList[this.categoryOn].type
			}
		},
		methods: {
			toPage,
			settingchange(k, v) {
				this.settingItem[k] = v;
				// this.$set(this.settingItem,k,v)
				this.dataList.forEach(item => {
					if ((item.id && item.id == this.settingItem.id) || (item.askrentId && item.askrentId == this
							.settingItem.askrentId) || (item.buystoreId && item.buystoreId == this.settingItem
							.buystoreId)) {
						console.log('sdfsfsf')
						item[k] = this.settingItem[k]
					}
				})
			},
			toVisitor(data) {
				uni.navigateTo({
					url: "/pages/aboutUsers/aboutUsers/visitor?mind=1&data=" + JSON.stringify(data)
				})
			},
			toOpear(message) {
				this.$util.Tips({
					title: message
				})
				this.page = 1;
				this.initList();
			},
			setMeth(data) {
				this.settingItem = data;
				this.setShow = true
			},
			toVip(data) {
				this.settingItem = data;
				console.log("this.settingItem", this.settingItem)
				let that = this;
				let ti = data.isVip && data.isVip == 1 ? '取消' : '开启';
				let st = data.isVip && data.isVip == 1 ? 0 : 1;
				uni.showModal({
					title: '提示',
					content: '是否' + ti + '信息VIP',
					success(res) {
						if (res.confirm) {
							that.$refs.setting.settingVip(st)
						}
					}
				})
			},
			toTopping(linkId, linkType,storeCategory,status) {
				this.linkId = linkId;
				this.linkTypes = linkType;
				this.infoStoreCategory = storeCategory;
				this.infoStatus = status;
				console.log('infoStatus',this.infoStatus)
				this.linkShow = true;
			},
			//托管方法
			tgMeth(id) {
				let that = this;
				console.log(id)
				uni.showModal({
					title: "提示",
					content: "是否立即托管",
					success(res) {
						if (res.confirm) {
							reardTrusteeship({
								id
							}).then(res => {
								if (res.code == 200) {
									that.$util.Tips({
										title: '托管成功'
									})
									setTimeout(() => {
										that.page = 1;
										that.initList();
									}, 1500)

								}
							}).catch(e => {
								that.$util.Tips({
									title: e
								}, {
									tab: 2,
									url: "/pages/users/user_money/index"
								})
							})
						}
					}

				})

			},
			//获取数据列表
			async getList() {
				try {
					let {
						data
					} = await AllListApi({
						page: this.page,
						limit: this.limit
					});
					uni.hideLoading()
					let arr = []
					// data.list.map(item => {
					// 	item.list.map(child => {
					// 		console.log((!child.status) && child.buystoreId, child.status, child
					// 			.buystoreId)
					// 		if ((!child.status) && child.askrentId) {
					// 			child.statusCode = 2
					// 		} else if ((!child.status) && child.buystoreId) {
					// 			child.statusCode = 4
					// 		}
					// 		arr.push(child)
					// 	})
					// })
					let d = data.list.map(item => {
						if (((!item.status) && item.askrentId) || item.status == 3) {
							item.statusCode = 2
						} else if (((!item.status) && item.buystoreId && item.buystoreId != 0)|| item.status == 4) {
							item.statusCode = 4
						}
						return item;
					})
					this.dataList = [...this.dataList, ...d];
					console.log("this.datalist", this.dataList)
				} catch (e) {
					console.log(e)
					//TODO handle the exception
				}
			},
			//出租 转让 出售列表
			async getOnlist() {
				try {
					const storeCategory = getCategory(this.categoryType) ? getCategory(this.categoryType).value : ''
					// const status = this.typeOn == 0 ? 0 : (this.typeOn == 1 ? 2 : (this.typeOn == 3 ? 1 : ''))
					const status = this.types[this.typeOn].type
					let {
						data
					} = await OnListApi({
						storeCategory,
						status,
						limit: this.limit,
						page: this.page
					});
					this.dataList = [...this.dataList, ...data.list]
					uni.hideLoading()
				} catch (e) {
					console.log(e)
					//TODO handle the exception
				}
			},
			//求租，求购
			async getHirePurchase() {
				console.log(getCategory(this.categoryType))
				// const storeCategory = this.categoryOn != 0 ? getCategory(this.categoryOn).value : ''
				let params = {
					page: this.page,
					limit: this.limit
				}
				if (getCategory(this.categoryType)) {
					console.log("this.categoryType", this.categoryType)
					params['storeCategory'] = getCategory(this.categoryType).value
				}
				let {
					data
				} = this.typeOn == 3 ? await getOnGroupListApi(params) : await onBuyListApi(params);
				let dc = data.list.map(item => {
					//1求租、2求购
					item.type = this.typeOn == 3 ? 1 : 2;
					if ((!item.statusCode) && item.askrentId) {
						item.statusCode = 2
					} else if ((!item.statusCode) && item.buystoreId) {
						item.statusCode = 4
					}
					return item
				})
				this.dataList = [...this.dataList, ...dc]
				uni.hideLoading()

			},
			typeNavClick(item, index) {
				// let title = '发布-'+this.categoryList[this.categoryOn].name+item.name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				this.typeOn = index
				if (this.types.length > 5) {
					var tempIndex = index;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabRight = (index - 2) * this.widthIng
				}
				this.page = 1;
				this.initList()
			},
			initList() {
				showLoading()
				if (this.page == 1) {
					this.dataList = []
				}
				console.log("this.categoryType", this.categoryType, this.categoryType === '')
				if (this.categoryType === '' && this.typeOn == 0) return this.getList()
				if ([1, 2, 4, 0].includes(this.typeOn)) return this.getOnlist()
				else if ([3, 5].includes(this.typeOn)) return this.getHirePurchase()
			},
			// 导航栏点击
			categoryNavClick(item, index) {
				// let title = '发布-'+item.name+this.types[this.typeOn].name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				if (this.categoryList.length > 6) {
					var tempIndex = index - 2;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
				}
				this.categoryOn = index //设置导航点击了哪一个
				this.isLeft = index * this.isWidth //设置下划线位置
				this.page = 1;
				this.initList()
			},
			jumpPage(item) {
				let enums = {
					883: 0,
					884: 1,
					1014: 2,
					911: 3,
					885: 4,
					912: 5,
					913: 6,
				}
				let categoryOnTab = enums[item.storeCategory];
				if (item.askrentId) {
					item.id = item.askrentId
				} else if (item.buystoreId) {
					item.id = item.buystoreId
				}
				console.log("typeOn", item)
				toPage('/pages/releases/subletOfShop/subletOfShop?categoryOn=' + categoryOnTab + '&typeOn=' + item
					.statusCode + '&id=' + item.id + '&mind =1' + '&storeCategory=' + item.storeCategory)
			},
			//刷新
			onRefresh() {
				// this.categoryOn = 0;
				// this.typeOn = 0;
				this.page = 1;
				this.dataList = [];
				this.getList()
			},
			lower() {
				this.page++;
				this.initList()
			},
			returns: function() {
				uni.navigateBack();
			},
		}
	}
</script>

<style lang="scss">
	.layout {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.scroll {
			overflow-y: auto;
			padding-bottom: 35rpx;
			flex: 1;

			.scroll-Y {
				height: 100%;
			}
		}

	}

	.header-top {
		height: 88rpx;
		background: rgba(238, 33, 45, 1);
		padding: 0rpx 30rpx;

		.header-top-tittle {
			line-height: 0rpx;
			font-size: 36rpx;
			color: rgba(255, 255, 255, 1);
		}
	}

	.nav-bg {
		background: #ffffff;
		padding: 18rpx 30rpx 36rpx;

		.item~.item {
			// margin-left: 48rpx;
		}

		.item {
			display: inline-block;
			white-space: nowrap;

			.img {
				width: 70rpx;
				height: 70rpx;
				border-radius: 50%;
				background: #ffffff;

				image {
					width: 42rpx;
					height: 44rpx;
				}
			}

			.name {
				width: 70rpx;
				font-size: 26rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: rgba(0, 0, 0, 1);
				text-align: center;
			}
		}

		.item-on {
			.img {
				background: rgba(247, 247, 247, 1);
			}

			.name {
				position: relative;
				color: rgba(239, 39, 50, 1);
				font-weight: 700;

				&::after {
					content: '';
					position: absolute;
					left: 0;
					bottom: 0;
					width: 100%;
					height: 50%;
					background-color: rgba(239, 39, 50, .2);
					border-radius: 10rpx;
				}
			}
		}
	}

	.type-bg {
		padding: 24rpx 30rpx;
		background: rgba(247, 248, 250, 1);

		.type~.type {
			margin-left: 22rpx;
		}

		.type {
			display: inline-block;
			white-space: nowrap;
			padding: 6rpx 24rpx;
			border-radius: 10rpx;
			border: 1px solid rgba(229, 229, 229, 1);
			font-size: 32rpx;
			font-weight: 400;
			color: rgba(128, 128, 128, 1);
		}

		.type-on {
			border: 1px solid rgba(238, 33, 45, 1);
			background: rgba(238, 33, 45, 1);
			color: #ffffff;

		}
	}

	.bottom {
		width: 412rpx;
		border-radius: 100px;
		background: rgba(238, 33, 45, 1);
		box-shadow: 0rpx 4rpx 20rpx 10rpx rgba(0, 0, 0, 0.05);
		line-height: 84rpx;
		font-size: 36rpx;
		color: rgba(255, 255, 255, 1);
		margin: 0 auto 20rpx;
	}
</style>